<template>
  <div>
    <div class="pageTopTitle">业务渠道管理</div>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
               label-width="80px"
      >
        <el-form-item label="渠道名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入渠道名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="渠道编号" prop="code">
          <el-input
            v-model="queryParams.code"
            placeholder="请输入渠道编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="app-container mt20">
      <el-row :gutter="10" class="mb20">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="channelList">
        <el-table-column label="渠道编号-名称" >
          <template slot-scope="scope">
            <div>{{ scope.row.code }}</div>
            <div>{{ scope.row.name }}</div>
          </template>
        </el-table-column>
        <el-table-column label="渠道登陆账号" prop="account"/>
        <el-table-column label="渠道类型" prop="type">
          <template slot-scope="scope">
            <span class="fs-12">{{ typeList.find(item => item.value === scope.row.type).label }}</span>
          </template>
        </el-table-column>
        <!--        <el-table-column label="状态" prop="status">-->
        <!--          <template slot-scope="scope">-->
        <!--            <span class="fs-12">{{ scope.row.status === 0 ? '待审核' : '审核通过' }}</span>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column label="渠道城市" prop="cityName"/>
        <el-table-column label="最新跟进">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleAddFollowUp(scope.row)"
            >新增跟进
            </el-button>
            <div style="background-color: #f5f5f5;padding: 5px;font-size: 12px;color: #999;width: fit-content">
              {{ scope.row.latestRecord || '暂无跟进记录' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="渠道数据统计" >
          <template slot-scope="scope">
            <div class="fs-12">
              <span >客户数：</span>
              <router-link :to="'/web/business/client/' + scope.row.id" class="link-type">
                <span  class="text-success fs-15 f-w-bold" style="text-decoration: underline;">{{ scope.row.clientNum }}</span>
              </router-link>
            </div>
            <div class="fs-12 mt6">
              <span >业绩（元）：</span>
              <router-link :to="'/web/business/project/' + scope.row.id" class="link-type">
                <span  class="text-success fs-15 f-w-bold" style="text-decoration: underline;">{{ scope.row.moneyNum }}</span>
              </router-link>
            </div>
            <div class="fs-12 mt6">
              <span >项目数：</span>
              <router-link :to="'/web/business/project/' + scope.row.id" class="link-type">
                <span  class="text-success fs-15 f-w-bold" style="text-decoration: underline;">{{ scope.row.projectNum }}</span>
              </router-link>
            </div>
            <div class="mt6">
              <span class="fs-12 mr5" style="color: #999093;">截止更新：{{ scope.row.updateNumTime || '未更新' }}</span>
              <el-button type="text" size="mini" style="color: #00b42b" @click="handleUpdateNum(scope.row)" >更新</el-button>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createAt" width="180"/>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改客户跟进记录对话框 -->
    <el-drawer
      title="添加跟进记录"
      :visible.sync="openFollowUp"
      size="35%"
      :wrapper-closable="false"
    >
      <div class="drawer-content">
        <el-form ref="followUpForm" :model="followUpForm" :rules="followUpRules" label-width="80px">
          <el-form-item label="跟进渠道" prop="channelId">
            <el-input v-model="followUpForm.channelName" disabled placeholder="请选择客户"></el-input>
          </el-form-item>
          <el-form-item label="跟进方式" prop="method">
            <el-select v-model="followUpForm.method" placeholder="请选择跟进方式" style="width: 100%;">
              <el-option label="电话" value="1"></el-option>
              <el-option label="短信" value="2"></el-option>
              <el-option label="上门拜访" value="3"></el-option>
              <el-option label="邮件" value="4"></el-option>
              <el-option label="其他" value="5"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="跟进内容" prop="content">
            <el-input type="textarea" rows="4" v-model="followUpForm.content" placeholder="请输入跟进内容"/>
          </el-form-item>
          <el-form-item label="跟进图片" prop="images">
            <ImageUpload :limit="3" v-model="followUpForm.images"/>
          </el-form-item>
        </el-form>
      </div>
      <div class="drawer-footer">
        <el-button type="primary" style="width: 120px;" @click="submitFollowUpForm" size="small">确 定</el-button>
        <el-button @click="cancelFollowUp" style="width: 120px;" size="small">取 消</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { listChannel, updateChannelNum } from '@/api/web/channel'
import { addUp } from '@/api/web/ChannelFollowUp'
import { provinceAndCityData } from 'element-china-area-data'
import { mapGetters } from 'vuex'
export default {
  name: 'Channel',
  computed: {
    ...mapGetters([
      'userId',
    ])
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 渠道表格数据
      channelList: [],
      // 是否显示弹出层
      openFollowUp: false,
      followUpForm: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        code: null,
        type: null,
        status: null
      },
      typeList: [
        { label: '机构', value: 1 },
        { label: '企业', value: 2 },
        { label: '政府', value: 3 },
        { label: '个人', value: 4 },
        { label: '其他', value: 5 }
      ],
      options: provinceAndCityData,
      userList: [],
      selectedOptions: [],
      // 跟进记录表单校验
      followUpRules: {
        method: [
          { required: true, message: '跟进方式不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '跟进内容不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    console.info(this.userId)
    this.queryParams.businessUserId = this.userId
    this.getList()
  },
  methods: {
    /** 查询渠道列表 */
    getList() {
      this.loading = true
      listChannel(this.queryParams).then(response => {
        this.channelList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    /** 新增跟进记录按钮操作 */
    handleAddFollowUp(row) {
      this.followUpForm = {
        channelName: row.name,
        channelId: row.id,
        method: null,
        content: null,
        images: null,
      }
      this.openFollowUp = true
    },

    cancelFollowUp() {
      this.openFollowUp = false
      this.followUpForm = {}
      this.resetForm('followUpForm')
    },

    submitFollowUpForm() {
      this.$refs['followUpForm'].validate(valid => {
        if (valid) {
          addUp(this.followUpForm).then(response => {
            this.$modal.msgSuccess('新增跟进记录成功')
            this.openFollowUp = false
            this.getList()
          })
        }
      })
    },

    handleUpdateNum(row){
      updateChannelNum(row.id).then(response => {
        this.$message.success('更新成功')
        this.getList()
      })
    },
  }
}
</script>
<style lang="scss" scoped>

.contact-info {
  display: flex;
  align-items: center;
  margin-bottom: 30px;

  .contact-item-name {
    // 文字竖直排列
    width: 20px;
    margin-right: 20px;
    font-size: 14px;
    text-align: center;
    border: 1px solid #165DFF;
    color: #165DFF;
    padding: 12px 0;
  }

  .btn-group {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
  }
}

.drawer-content {
  padding: 0 30px 80px 20px;
}

.drawer-footer {
  position: absolute;
  bottom: 0;
  box-sizing: border-box;
  text-align: right;
  padding: 16px;
  width: 100%;
  border-top: 1px solid #eee;
  background-color: #fff;
}

::v-deep .el-form-item__label {
  font-weight: 500 !important;
}

::v-deep .el-form--label-top .el-form-item__label {
  padding: 0 0 4px 0 !important;
}

::v-deep .el-divider--horizontal {
  margin: 40px 0 !important;
}

</style>
